<?php
/**
 * Zuka Controller
 * @author Sros
 */
class Zcontroller extends CI_Controller {
    protected $z_tran = null;
	/**
	 * Constructor of this controller
	 */
    function __construct() {
    	parent::__construct();
    	
    	date_default_timezone_set("Asia/Phnom_Penh");
    	$this->initController();
    	$this->load_translation(array("default"));
    	$this->load->library("OnlineUsers");
    	$this->load->model("m_classify");
    	$this->load->model("m_usertracking");

    	
    }
    
    private function initController() {
        $CI =& get_instance();
		define("CHAR_SET", $CI->config->item("charset"));
		$this->load->helper("html");
        $this->load->helper('url_helper');
        $this->load->helper('zuka_helper');
        
        require_once BASEPATH.'core/Model.php';
        require_once APPPATH.'libraries/Zmodel.php';
        require_once APPPATH.'libraries/Zsession.php';
        require_once APPPATH.'libraries/Zencryption.php';
        require_once APPPATH.'libraries/Translation.php';
        require_once APPPATH.'libraries/Zimage.php';
        
        
        
        session_start();
        $this->load->model("m_translate");
        if (!isset($_SESSION[Zsession::SESSION_KEY])) {
            $_SESSION[Zsession::SESSION_KEY] = array();
            $_SESSION[Zsession::SESSION_KEY][Zsession::SESSION_DATA_KEY] = array();
        }
        
        if (!isset($_SESSION[Zsession::SESSION_KEY][Zsession::SESSION_TRAN_KEY])) {
            $_SESSION[Zsession::SESSION_KEY][Zsession::SESSION_TRAN_KEY] = array();
            $lan_default = $this->m_translate->getDefaultLanuage(APPLICATION_ID);
            Zsession::setLanguage($lan_default["lan_id"], $lan_default["lan_code"]);
        }
        
        if (isset($_SESSION[Zsession::SESSION_KEY]["curURL"])) {
            $_SESSION[Zsession::SESSION_KEY]["preURL"] = $_SESSION[Zsession::SESSION_KEY]["curURL"];
        }
        
        Zsession::setCurURL(curPageURL());
        $this->z_tran = Translation::getInstance();
        
    }
    
	function load_translation($pages = array()) {
	  
	  if (is_array($pages) && count($pages) > 0) {
	      $object_translation = $this->m_translate->getObjects($pages, Zsession::getLanguageId());
	      Translation::getInstance()->merge_object($object_translation);
	  }
	 }
	 
	 function getVisitor(){
	      /* Hits table has an auto-incrementing id and an ip field */

		  // Grab client IP
		  //$ip = $_SERVER['REMOTE_ADDR'];
		  $ip = $this->input->ip_address();
			
		  // Check for previous visits
		  $query = $this->db->get_where('hits_counter', array('ip' => $ip), 1, 0);
		  $query = $query->row_array();
			
		  if (count($query) < 1)
			{
			    // Never visited - add
			    $date = new DateTime();
			    $this->db->insert('hits_counter', array('ip' => $ip, 'hits_date' => $date->format('Y-m-d H:i:s')) );
			} 
			
		  $hits=$this->db->count_all('hits_counter'); 
	 }
	 
}